package com.example.goods.service.impl;

import com.example.goods.bean.Shipping;
import com.example.goods.common.ResultResponse;
import com.example.goods.repository.ShippingRepository;
import com.example.goods.service.ShippingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Optional;

/**
 * Created by IntelliJ IDEA.
 *
 * @author ouyangzhixiang
 * @date 2018/7/5
 */
@Service
public class ShippingServiceImpl implements ShippingService {
    @Autowired
    private ShippingRepository repository;

    /**
     * 添加收货地址
     * @param userId
     * @param shipping
     * @return
     */
    @Override
    public ResultResponse add(Integer userId, Shipping shipping) {
        shipping.setUserId(userId);
        Shipping addShipping = repository.save(shipping);
        return ResultResponse.createBySuccessMessage("新建地址成功",addShipping);
    }

    /**
     * 查找收货地址
     * @param id
     * @return
     */
    @Override
    public ResultResponse<Optional<Shipping>> select(Integer id) {
     Optional<Shipping>  shipping= repository.findById(id);
     if (shipping.isPresent()){
         return ResultResponse.createBySuccess(shipping);
     }
            return ResultResponse.createByErrorMessage("无法查到该收件地址");

    }

}
